package simulator;

public class Ventilation 
{
    //variables
    private double ventilationFactor;           // (W/m3)
    private double area;                        //Hus areal (m2)
    private double wallsHeight;                 
    private double volume;                      //Hus volum
    private double airChanges;
    private int roomType;
    
    private double ventilationHour;             //(W)
    
    private double densityOfAir;                //density of air(kg/m3)
    private double absolutePreasure;            //absolute preasure (Pa)
    private final double R = 287.058;           //specific gas constant for dry air (J/(kg*K))
    private double T;                           //absolut temperatur (K)
    
    private double specificHeatCapacitiesOfAir; //Specific Heat Capacities of Air (J/(kg*K)
    
    private double volumeFlowRate;              //(m3/s)
    private double massFlowRate;                //(kg/s)
    private double sensibleHeatGain;            //(W)
    private double temperaturIn;                //(C)
    private double temperaturOut;               //(C)
    private double heatRecoveryEfficiency;      //(%)
    
    //MK2
    private double specificHeatFactorForAir;    //
    private double naturalVentilationHeatLoss;  //(W)
    private double numberOfAirChangesPerHour;   //(antall)
    //MK2
    
    //end of variables
    
    public Ventilation()
    {
    }
    
    /**
     * Centilation constructor
     * @param _area hus størrelse i m2
     * @param _wallsHeight
     * @param _temperatureDifference temperatur forskjell i C
     * @param _roomType 0 - små hus, 1
     */
    public Ventilation(double _area, double _wallsHeight, double _temperaturIn, double _temperaturOut, double _absolutePreasure, int _roomType)
    {
        area = _area;
        wallsHeight = _wallsHeight;
        roomType = _roomType;
        temperaturIn = _temperaturIn;
        temperaturOut = _temperaturOut;
        absolutePreasure = _absolutePreasure;
        ventilationFactor = 0.33;
        volume = area * wallsHeight;
        heatRecoveryEfficiency = 80;
        
        numberOfAirChangesPerHour = 0.4;
        
    }//end of constructor
    
    /**
     * Klass innholder ikke alt, skal videreutvikles!!!
     */
    private void airExchangeDependingOnTheTypeOfRoom()
    {
        if (roomType == 0)
        {
            airChanges = 2;
        }
        else if(roomType == 1)
        {
            airChanges = 4;
        }
        else
        {
            airChanges = 4;
        }
    }
    
    /**
     * Beregner energitap på grunn av ventilasjon
     * @return energi (W)
     */
    public double CalculationTime()
    {
        //airExchangeDependingOnTheTypeOfRoom();  //Set airChanges factor
        //ventilationHour = volume * airChanges * temperatureDifference * ventilationFactor;
        /*
        SpecificHeatCapacitiesOfAir();
        System.out.println("heat capacity of air " + specificHeatCapacitiesOfAir);
        System.out.println("density " + DensityOfAir(temperaturOut));
        System.out.println("volume flow rate " + VolumeFlowRate());
        System.out.println("T in " + temperaturIn);
        System.out.println("T out " + temperaturOut);
        ventilationHour = (1 - heatRecoveryEfficiency/100) * specificHeatCapacitiesOfAir * DensityOfAir(temperaturOut) * VolumeFlowRate() * (temperaturIn - temperaturOut);
        */
        ventilationHour = NaturalVentilationHeatLoss();
        
        return ventilationHour;
    }
    
    private double DensityOfAir(double _temperatur)
    {
        densityOfAir = absolutePreasure / (R * (_temperatur + 273.5));
        return densityOfAir;
    }
    
    private void SpecificHeatCapacitiesOfAir()
    {
        if(temperaturOut <= -100)
            specificHeatCapacitiesOfAir = 1026;
        else if(temperaturOut > -100 && temperaturOut < -50 )
            specificHeatCapacitiesOfAir = 1009;
        else if(temperaturOut >= -50 && temperaturOut <= 40)
            specificHeatCapacitiesOfAir = 1005;
        else
            specificHeatCapacitiesOfAir = 1009;
    }
    
    private double VolumeFlowRate()
    {
        SensibleHeatGain();
        massFlowRate = sensibleHeatGain / ((specificHeatCapacitiesOfAir/1000) * (temperaturIn - temperaturOut));
        //System.out.println("mass flow rate " + massFlowRate);
        volumeFlowRate = massFlowRate / DensityOfAir(temperaturOut);
        return volumeFlowRate;
    }
    
    private void SensibleHeatGain()
    {
        if (roomType == 0)
            sensibleHeatGain = 70;
        /*
         * Offices, hotels, apartments, department store, retail store, Bank
           sensibleHeatGain =75
	
           Restaurant, Factory
           sensibleHeatGain = 80

           Gymnasium
           sensibleHeatGain = 210
         */
    }
    
    
    //***MK 2***
    private double SpecificHeatFactorForAir()
    {
        SpecificHeatCapacitiesOfAir();
        specificHeatFactorForAir = specificHeatCapacitiesOfAir * DensityOfAir(temperaturOut) / 3600;//3600 to convert from hours to seconds
        /*System.out.println("heat capacity of air " + specificHeatCapacitiesOfAir);
        System.out.println("density " + DensityOfAir(temperaturOut));
        System.out.println("Specific Heat Factor For Air" + specificHeatFactorForAir);*/
        return specificHeatFactorForAir;
    }
    
    private double NaturalVentilationHeatLoss()
    {
        naturalVentilationHeatLoss = numberOfAirChangesPerHour * area * wallsHeight * SpecificHeatFactorForAir() * (temperaturIn - temperaturOut);
        /*System.out.println("numberOfAirChangesPerHour " + numberOfAirChangesPerHour);
        System.out.println("area " + area);
        System.out.println("wallsHeight " + wallsHeight);
        System.out.println("T in " + temperaturIn);
        System.out.println("T out " + temperaturOut);
        System.out.println("natural Ventilation Heat Loss " + naturalVentilationHeatLoss);*/
        return naturalVentilationHeatLoss;
    }
    //***MK 2***
    
    
}//end of ventilation
